/**
 * @FileName: SegmentUtils.java
 * @Package: top.swimmer.demo.nlp
 * @author youshipeng
 * @created 2017/5/2 16:29
 * <p>
 * Copyright 2016 ziroom
 */
package top.swimmer.demo.nlp;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.hankcs.hanlp.HanLP.Config.CustomDictionaryPath;
//import static com.hankcs.hanlp.utility.Predefine.HANLP_DATA_ROOT_PATH;

/**
 * <p></p>
 *
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author youshipeng
 * @since 1.0
 * @version 1.0
 */
public class SegmentUtils {
    private static Segment NLPSegment;
    private static final String mobilePattern = "((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}";

    static {
//        HANLP_DATA_ROOT_PATH = "E:\\elasticsearch-2.3.3\\plugins\\HanLP\\";

        CustomDictionaryPath = new String[] {
                "E:\\elasticsearch-2.3.3\\plugins\\HanLP\\data\\dictionary\\custom\\CustomDictionary.txt"
        };
        NLPSegment = HanLP.newSegment()
                .enableCustomDictionary(true) // 用户词典
                .enablePartOfSpeechTagging(true) //词性标注
                .enableOffset(true) //计算偏移量
                .enableNameRecognize(true) // 中文人名识别
                .enableJapaneseNameRecognize(true) // 日本人名识别
                .enableNumberQuantifierRecognize(true) //数量词识别
                .enableOrganizationRecognize(true) //机构名识别
                .enableTranslatedNameRecognize(true); //音译人名识别
    }

    public static List<Term> seg(String input) {
        return NLPSegment.seg(input);
    }

    public static String getMaintainThing(String input) {
        for (Term term : NLPSegment.seg(input)) {
            if (term.nature.name().equals("n_ziroom_maintain")) {
                return term.word;
            }
        }
        return null;
    }

    public static String getPersonName(String input) {
        for (Term term : NLPSegment.seg(input)) {
            if (term.nature.name().startsWith("nr")) {
                return term.word;
            }
        }
        return null;
    }

    public static String getMobile(String input) {
        Pattern pat = Pattern.compile(mobilePattern);
        Matcher matcher = pat.matcher(input);
        if (!matcher.find()) {
            return null;
        }
        return matcher.group();
    }
}